/*
 * -------------------------------------------------------------------------------
 * @FilePath     : app_lpt.c
 * @Description  : application
 * Copyright (c) 2025 by NanoIC, All Rights Reserved. 
 * @Date         : 2025-03-12 11:59:26
 * @Author       : LvH
 * -------------------------------------------------------------------------------
 */
#include "lpt.h"
#include "wn8012_ck801.h"
#include "pin.h"
#include "pm.h"
#include <drv/iwdt.h>
#include "app_uart.h"

uint8_t lpt_intrrupt_flag = 1;
extern uint8_t app_stop_wait_flag;
/**
 * @brief LPT使能周期中断
 * 
 * @param NewState enable/disable
 * @param LPT_IMSCR_X 中断类型
 */
//void LPT_ConfigInterrupt_CMD(bool NewState,LPT_IMSCR_TypeDef LPT_IMSCR_X)
//{
//    if (NewState != false)
//    {
//        LPT->IMCR  |= LPT_IMSCR_X;						
//    }
//    else
//    {
//        LPT->IMCR  &= ~LPT_IMSCR_X;					
//    }
//}

/**
 * @brief lpt中断使能
 * 
 */
void lpt_int_enable(void)
{
    INTC_ISER_WRITE(LPT_INT);
}

/**
 * @brief 低功耗定时器配置
 * 
 * @param cycle_ms 执行周期_ms
 */
void app_lpt_init(uint16_t cycle_ms)
{
    // LPT_DeInit();
    LPT->CEDR 		= 0xBE980000;
    LPT->RSSR 		= LPT_RESET_VALUE;
    LPT->PSCR 		= LPT_RESET_VALUE;
    LPT->CR	  		= 0X00010010;
    LPT->SYNCR 		= LPT_RESET_VALUE;
    LPT->PRDR 		= LPT_RESET_VALUE;
    LPT->CMP 		= LPT_RESET_VALUE; 
    LPT->CNT 		= LPT_RESET_VALUE;
    LPT->TRGFCR 	= LPT_RESET_VALUE;
    LPT->TRGFWR 	= LPT_RESET_VALUE;
    LPT->EVTRG 		= LPT_RESET_VALUE;
    LPT->EVPS		= LPT_RESET_VALUE;
    LPT->EVSWF 		= LPT_RESET_VALUE;
    // LPT->RISR 		= LPT_RESET_VALUE;
    // LPT->MISR 		= LPT_RESET_VALUE;
    LPT->IMCR 		= LPT_RESET_VALUE;
    LPT->ICR 		= LPT_RESET_VALUE;
    // LPT_Configure(LPTCLK_EN,LPT_ISCLK,LPT_IMMEDIATE,LPT_PSC_DIV4,0,LPT_OPM_CONTINUOUS);//选择27K 4分频

    //选择27K分频  16分频
    LPT->CEDR |=	1| 2| 4| 64| (0<<8);
    LPT->PSCR = 4;                              
    LPT->CR |=0;

    // LPT_Period_CMP_Write(27000,0);               //周期4s一次中断，时钟源27K，定时时间 = 27K/4*27000
    LPT->PRDR 	= cycle_ms * 27000/16/1000;
    LPT->CMP 	= cycle_ms * 27000/16/1000 - 1;
    LPT_ConfigInterrupt_CMD(true,LPT_MATCH);      //使能周期中断
    lpt_int_enable();                           //使能LPT中断
    
    SYSCON->WKCR |= 1<<10;     //LPT_WKEN
    SYSCON->GCER |= 1<<12;     //使能Deepsleep模式下ISCLK继续工作
    INTC_IWER_WRITE(LPT_INT);  //使能LPT唤醒 
}

/**
 * @brief lpt中断处理函数
 * 
 */
void lpt_intrrupt_handle(void)
{
    uint32_t irq = csi_irq_save();
    lpt_intrrupt_flag = 1;
    csi_irq_restore(irq);
}

/**
 * @brief 检查是否需要发送心跳包
 * 
 * @return uint8_t 
 */
uint8_t lpt_heart_status_get(void)
{
    uint8_t flag_buf = 0;
    uint32_t irq = csi_irq_save();
    flag_buf = lpt_intrrupt_flag;
    csi_irq_restore(irq);
    return flag_buf;
}

/**
 * @brief 设置心跳包状态
 * 
 */
void lpt_heart_status_set(uint8_t flag)
{
    uint32_t irq = csi_irq_save();
    lpt_intrrupt_flag = flag;
    csi_irq_restore(irq);
}

/**
 * @brief app_lpt_start
 * 
 */
void app_lpt_start(void)
{
    LPT->RSSR |= 0X01;
    lpt_heart_status_set(2);
}

/**
 * @brief app_lpt_stop
 * 
 */
void app_lpt_stop(void)
{
	LPT->RSSR &= 0XFFFFFFFE;
    LPT->CNT = 0;
}


///******************************************************************************************** */
/**
 * @brief app_sleep_heart_beat
 * 
 */
void app_sleep_heart_beat(void)
{
    app_lpt_start();
    //进入休眠
    while(1)
    {
		if(app_stop_wait_flag)
		{
			break;
		}
        if(lpt_heart_status_get() == 1)
        {
			
//			app_uart_doze_and_weakup_io_set(true);
            //TODO 低功耗发送心跳包
            one_wrie_bus_app_send_heart_beat(1);
            //TODO
//			 while(release_tx_flag==0)
//			 {
//				 
//			 };
			mdelay(2);
			if(release_tx_flag == 0x01)
			{
				kb_serial++;
				release_tx_flag = 0;
				app_uart_tx_io_set(false);
			}
			
//			app_uart_doze_and_weakup_io_set(false);
            lpt_heart_status_set(0);
			if(app_stop_wait_flag)
			{
				app_stop_wait_flag = 0;
			}
            csi_pm_enter_sleep(PM_MODE_DEEPSLEEP);
        }else if (lpt_heart_status_get() == 2)
        {
            //首次进入deep_sleep
            csi_pm_enter_sleep(PM_MODE_DEEPSLEEP);
        }else{
            //唤醒
            break;
        }
    }
    //退出休眠
    app_lpt_stop();
    lpt_heart_status_set(0);
}








